package com.ht.right.service.impl;

import java.io.Serializable;
import java.lang.reflect.Method;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;

import org.dom4j.Element;

import com.ht.common.entity.PageInfo;
import com.ht.right.entity.Menu;
import com.ht.right.service.CommonFacadeService;
import com.ht.right.service.CommonService;
import com.ht.right.service.MenuService;
import com.ht.right.service.RightService;
import com.ht.right.util.CommonUtil;

/**
 * 通用方法外观模型服务层
 * 
 * @author 庞成祥
 * 
 */
public class CommonFacadeServiceImpl<T> implements CommonFacadeService<T> {
	private CommonService<T> commonService;
	private MenuService menuService;
	private RightService rightService;

	@Override
	public List<Map<String, String>> loadAllList(String menuId) {
		Menu menu = menuService.findMenuById(menuId);
		if (CommonUtil.isNullOrEmpty(menu)) {
			return null;
		}
		List<T> objList = commonService.loadAllList(menu.getEntityClsName());
		String bodyHtml = "<root>" + menu.getMenuHtmlBody() + "</root>";
		List<Element> eleList = CommonUtil.getXmlChildElements(bodyHtml);
		Set<String> fieldSet = new HashSet<String>();
		for (Element e : eleList) {
			if (CommonUtil.isNullOrEmpty(e.attributeValue("field"))) {
			} else {
				fieldSet.add(e.attributeValue("field"));
			}
		}
		return commonService.dataList(objList, fieldSet);

	}

	@Override
	public Map<String, Object> loadListForPage(String menuId, PageInfo pageInfo, String orgIds) {
		Menu menu = menuService.findMenuById(menuId);
		if (CommonUtil.isNullOrEmpty(menu)) {
			return null;
		}
		System.out.println(2);
		Set<String> docSet = rightService.getDocIds(orgIds, menu.getEntityClsName());
		String docIds = rightService.convertToStr(docSet);
		List<T> objList = commonService.getPageDataList(menu.getEntityClsName(), pageInfo, docIds);
		int count = commonService.getAllDataCount(menu.getEntityClsName(), docIds);
		String bodyHtml = "<root>" + menu.getMenuHtmlBody() + "</root>";
		List<Element> eleList = CommonUtil.getXmlChildElements(bodyHtml);
		Set<String> fieldSet = new HashSet<String>();
		for (Element e : eleList) {
			if (CommonUtil.isNullOrEmpty(e.attributeValue("field"))) {
			} else {
				fieldSet.add(e.attributeValue("field"));
			}
		}
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("total", count);
		map.put("data", commonService.dataList(objList, fieldSet));
		return map;
	}

	@Override
	public List<T> getPageDataList(String entityClsName, PageInfo pageInfo, String docIds) {
		return commonService.getPageDataList(entityClsName, pageInfo, docIds);
	}

	@Override
	public void save(T o, String rightId) {
		// TODO Auto-generated method stub
		commonService.save(o);
		if(rightId!=null){
			try {
				Menu menu = menuService.findMenuById(rightId);
				Method m = (Method) o.getClass().getMethod("getUuid");
				String uuid = (String) m.invoke(o);
				//System.out.println(o.getClass().getName());
				rightService.addRight(uuid, o.getClass().getSimpleName(), menu.getDataRightJson());
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
	}

	@Override
	public void delete(T o) {
		commonService.delete(o);
	}

	@Override
	public void update(T o,String rigthData) {
		commonService.update(o);
		if (CommonUtil.isNullOrEmpty(rigthData)) {
		} else {
			if ("[]".equals(rigthData)) {
			} else {
				try {
					Method m = (Method) o.getClass().getMethod("getUuid");
					String uuid = (String) m.invoke(o);
					rightService.updateRight(uuid, o.getClass().getSimpleName(), rigthData);
				} catch (Exception e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
				
			}
		}
	}

	@Override
	public void saveOrUpdate(T o) {
		// TODO Auto-generated method stub
		commonService.saveOrUpdate(o);
	}


	public CommonService<T> getCommonService() {
		return commonService;
	}

	public void setCommonService(CommonService<T> commonService) {
		this.commonService = commonService;
	}

	public MenuService getMenuService() {
		return menuService;
	}

	public void setMenuService(MenuService menuService) {
		this.menuService = menuService;
	}

	public RightService getRightService() {
		return rightService;
	}

	public void setRightService(RightService rightService) {
		this.rightService = rightService;
	}

	@Override
	public T get(Class<T> c, Serializable id) {
		// TODO Auto-generated method stub
		return commonService.get(c, id);
	}

	@Override
	public T getEntityByID(String entityClsName, String uuid) {
		// TODO Auto-generated method stub
		return commonService.getEntityByID(entityClsName, uuid);
	}

	@Override
	public void deleteId(String model,String attr,String param){
		commonService.deleteId(model, attr, param);
	}
}
